<?php

use yii\helpers\Html;
use yii\grid\GridView;

use app\models\MGoods;
use app\models\MGoodsSearch;
use yii\helpers\ArrayHelper;
use app\models\Category;
use yii\helpers\Url;

use app\models\MGoodscat;
/* @var $this yii\web\View */
/* @var $searchModel app\models\MGoodsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

use app\models\U;

$this->title = '商品列表';
//$this->params['breadcrumbs'][] = $this->title;
$cat = \app\models\Category::findOne(['id' =>$_GET['goods_kind']]);
?>
<div class="mgoods-index">

    <h4><?= Html::encode($this->title) ?> | <?= empty($cat)?'':$cat->name; ?></h4>

    <p align="left">
            <button id="compareBtn" class="btn btn-lg btn-success">
                <span class="glyphicon glyphicon-duplicate"></span>&nbsp; 对比
            </button>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <!--
    <p>
        <//?= Html::a('新增商品', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    -->

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'tableOptions' =>['class' => 'table table-striped'],
        'summary'=>'',
        //当没数据时，不显示表头
        //'showOnEmpty'=>true,

        //'layout'=>"{sorter}\n{pager}\n{summary}\n{items}",

        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],

            //'goods_id',
            /*
            [
                'attribute' => 'goods_id',
                'label' => '商品编号',
                'headerOptions' => array('style'=>'width:50px;'),
            ],
            */
            [
                //'attribute' => 'compare',
                'label' => '对比',
                'format' => 'raw',
                'value'=>function ($model, $key, $index, $column) {
                    $checkStr = "<input name='compare' type='checkbox' value=".$model->goods_id.">";
                    return $checkStr;
                },
                'headerOptions' => array('style'=>'width:60px;'),
            ],

            /*
            [
                'attribute' => 'list_img_url',
                'label' => '',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                    //return '<img src=' . $model->list_img_url .' width=120px height=90px>';
                    return Html::a('<img src=' . $model->list_img_url .' width=120px height=90px>', 'index.php?r=site%2Fclient-goods-view&id='.$model->goods_id);
                },
                'filter'=> false,
                'headerOptions' => array('style'=>'width:80px;'),        
            ],
            'title',
            */
            [
                'attribute' => 'title',
                'label' => '商品名',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                    //return '<img src=' . $model->list_img_url .' width=120px height=90px>';
                    return Html::a('<img src=' . $model->list_img_url .' width=100px height=70px><br>'.$model->title, 'index.php?r=site%2Fclient-goods-view&id='.$model->goods_id);
                },
                //'filter'=> false,
                'headerOptions' => array('style'=>'width:180px;'),
            ],

            [
                'attribute' => 'model',
                'label' => '型号',
                'headerOptions' => array('style'=>'width:120px;'),
            ],

            //'goods_subkind',
            [
                'attribute' => 'goods_subkind',
                'label' => '二级分类',
                'value'=>function ($model, $key, $index, $column) {
                    return MGoods::getKindName($model->goods_subkind);
                },
                'filter' =>ArrayHelper::map(Category::find()->where(['parent_id'=> $_GET['goods_kind']])->asArray()->all(),'id','name'),
                'headerOptions' => array('style'=>'width:120px;'),
            ],

            //'goods_3rdkind',
            [
                'attribute' => 'goods_3rdkind',
                'label' => '三级分类',
                'value'=>function ($model, $key, $index, $column) {
                    return MGoods::getKindName($model->goods_3rdkind);
                },
                'filter'=> array(),
                'headerOptions' => array('style'=>'width:120px;'),
            ],

            //'brand',
            [
                'attribute' => 'brand',
                'label' => '品牌',
                'value'=>function ($model, $key, $index, $column) {
                    return MGoods::getBrandOption($model->brand);
                },
                'filter'=> MGoods::getBrandOption(),
                'headerOptions' => array('style'=>'width:120px;'),
            ],

            //'prod_area',
            [
                'attribute' => 'prod_area',
                'label' => '产地',
                'headerOptions' => array('style'=>'width:60px;'),
            ],

            //'goods_kind',
            //'descript',
            [
                'attribute' => 'pub_type',
                'label' => '发布类型',
                'value'=>function ($model, $key, $index, $column) {
                    return MGoods::getPubTypeOption($model->pub_type);
                },
                'filter'=> MGoods::getPubTypeOption(),
                'headerOptions' => array('style'=>'width:60px;'),
            ],
            [
                'attribute' => 'goods_neworold',
                'label' => '成色',
                'value'=>function ($model, $key, $index, $column) {
                    //若发布类型为租赁，则商品成色为0空 ， 1新品2二手
                    if($model->pub_type == 1)
                        return '--';
                    else
                        return MGoods::getGoodsNeworoldOption($model->goods_neworold);
                },
                'filter'=> MGoods::getGoodsNeworoldOption(),
                'headerOptions' => array('style'=>'width:60px;'),
            ],

            [
                'attribute' => 'create_time',
                'label' => '发布时间',
                'format' =>  ['date', 'php:Y-m-d H:i:s'],
                //'format' =>  ['date', 'php:Y-m-d'],
                'headerOptions' => array('style'=>'width:100px;'),
            ],


            //'price',
            //[
            //    'attribute' => 'price',
            //    'headerOptions' => array('style'=>'width:80px;'),
            //],

            //'price_hint',
            //'price_old',
            //[
             //   'attribute' => 'price_old',
              //  'headerOptions' => array('style'=>'width:80px;'),
            //],            
            // 'price_old_hint',
            // 'detail:ntext',
            // 'list_img_url:url',


            // 'body_img_url:url',
            /*
            [
                'attribute' => 'body_img_url',
                'label' => '大图',
                'format' => 'html',
                'value' => function($model, $key, $index, $column){
                        $len = 0;
                        $imgHtml = "";
                        $imgs = explode(";",$model->body_img_url);
                        foreach ($imgs as $img) {
                            $len++;
                            if(sizeof($imgs) == $len) break; //分号分割后，数组最后一项为空，剔除
                            $imgHtml = $imgHtml . '<img src=' . $img . ' width=45px height=45px>';
                        }
                        return $imgHtml;
                },    
                'headerOptions' => array('style'=>'width:160px;'),      
            ],
            */


            // 'quantity',
            // 'office_ctrl',
            // 'package_ctrl',
            //'detail_ctrl',
			/*
            [
                'attribute' => 'detail_ctrl',
                'label' => '显示详情',
                'value'=>function ($model, $key, $index, $column) { 
                        return MGoods::getDetailCtrlOption($model->detail_ctrl); 
                 },
                'filter'=> MGoods::getDetailCtrlOption(),
                'headerOptions' => array('style'=>'width:90px;'),
            ],
			*/
            // 'pics_ctrl',

            /*
            [
                'attribute' => 'status',
                'format' => 'html',
                'label' => '发布状态',
                'value'=>function ($model, $key, $index, $column) { 
                        if($model->status == 0)
                            return "<span style='color:red'>".MGoods::getStatusOption($model->status)."</span>"; 
                        else
                            return "<span style='color:green'>".MGoods::getStatusOption($model->status)."</span>"; 
                 },
                'filter'=> MGoods::getStatusOption(),
                'headerOptions' => array('style'=>'width:120px;'),
            ],
            */


            /*
            [
                'class' => 'yii\grid\ActionColumn',

                'template' => '{client-goods-view}',
                'buttons' => [
                    'client-goods-view' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('yii', 'View'),
                            'data-pjax' => '0',
                        ]);
                    }
            
                ],
            ],
            */




        ],
    ]); ?>

</div>


<script src="js/jquery.min.js"></script>
<script type="text/javascript">
    var a = new Array();
    var aStr = '';

    function removeByValue(arr, val) {
        for(var i=0; i<arr.length; i++) {
            if(arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    }

    function load_3rdkind() {
        var host_url = '<?php echo Url::to(["goods/category"]); ?>';

        htmldata=$.ajax({url:host_url+"&typeid=2&pid="+$("select[name='MGoodsSearch[goods_subkind]']").val(),async:false});
        $("select[name='MGoodsSearch[goods_3rdkind]']").html(htmldata.responseText);
        //alert("!!!!!!!!"+htmldata.responseText);
    }

    $(document).ready(function() {
            load_3rdkind();

            $("input[type='checkbox']").bind("click",function(){

                if($(this).is(':checked')) {
                    //alert('checked'+$(this).val());
                    a.push($(this).val());
                }
                else
                {
                    //alert('uncheck'+$(this).val());
                    removeByValue(a, $(this).val());
                }

            });

             $("select[name='MGoodsSearch[goods_subkind]']").change(function(){
                 //alert($("select[name='MGoodsSearch[goods_subkind]']").val());
                 load_3rdkind();
             });

            $("#compareBtn").click(function(){
                if(a.length < 2 ) {
                    alert("至少选择两个商品进行比较.");
                    return false;
                }

                if(a.length > 3) {
                    alert("最多同时支持3个商品进行比较.");
                    return false;
                }

                if(a.length ==2)
                    location.href = 'index.php?r=site/client-goods-compare&gid1='+a[0]+'&gid2='+a[1]+'&gid3=-1';
                else if(a.length ==3)
                    location.href = 'index.php?r=site/client-goods-compare&gid1='+a[0]+'&gid2='+a[1]+'&gid3='+a[2];
            })
    });
</script>
